package com;
import java.io.File;
import javax.ws.rs.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;


@Path("helloworld")
public class test {

	public File img = null;

	
	@GET
    @Produces("text/plain")
    public String getMessage(@QueryParam("json") String json,@QueryParam("i") int i,@QueryParam("machineid") String machineid) {
    JSONObject j=(JSONObject)JSONValue.parse(json);
       
    
	SessionFactory s1=new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
	
try{
	

    	for(int i1=0; i1<=i; i1++)
    	{Session session=s1.openSession();
    	processor p1=new processor();
    		
    			
    		String key="CPU" + i1;
    		
    		String str= (String) (j.get(key));
    		int value=Integer.parseInt(str);
    		p1.setMachineid(machineid);
    		p1.setCpuid(key);
    		p1.setCpuusage(value);    		
    		Transaction t=session.beginTransaction();
			session.save(p1);
			t.commit();
    	
			System.out.println(key);
    		System.out.println(value);
        	System.out.println("save");
    		
        	session.close();
    	}
		

    	s1.close();

    	System.out.println("evas");
		}
		catch(Exception e){
			System.out.println("" + e);
		}
		    	return "adc";
		    }
    /*
    @Path("chart")
    @GET 
    //@Produces("text/plain")
    @Produces("application/png")
    public Response getvipul()
    {
    	 File image = null;
    	DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    	processor p=new processor();
    	
		dataset.setValue(6, "Profit1", "Jane");
		dataset.setValue(3, "Profit2", "Jane");
		dataset.setValue(7, "Profit1", "T");
		dataset.setValue(10, "Profit2", "T");
		dataset.setValue(8, "Profit1", "Jill");
		dataset.setValue(8, "Profit2", "Jill");
		dataset.setValue(5, "Profit1", "John");
		dataset.setValue(6, "Profit2", "John");
		dataset.setValue(12, "Profit1", "F");
		dataset.setValue(5, "Profit2", "F");
		
		// Profit1, Profit2 represent the row keys
		// Jane, Tom, Jill, etc. represent the column keys
		
		JFreeChart chart = ChartFactory.createBarChart3D(
		   "Comparison between Salesman",  // Chart name
		   "Salesman",                     // X axis label
		   "Value ($)",                    // Y axis value
		   dataset,                        // data set
		   PlotOrientation.VERTICAL,
		   true, true, false);
	 try
		 {
		 image = File.createTempFile("image", "tmp");
		
		 // Assume that we have the chart
		 ChartUtilities.saveChartAsPNG(image, chart, 500, 300);
		/*
		 FileInputStream fileInStream = new FileInputStream(image);
		 OutputStream outStream = response.getOutputStream();   
		
		 long fileLength;
		 byte[] byteStream;
		
		 fileLength = image.length();
		 byteStream = new byte[(int)fileLength];
		 fileInStream.read(byteStream, 0, (int)fileLength);
		
		 response.setContentType("image/png");
		 response.setContentLength((int)fileLength);
		 response.setHeader("Cache-Control", 
		     "no-store,no-cache, must-revalidate, post-check=0, pre-check=0");
		 response.setHeader("Pragma", "no-cache");
		
		 fileInStream.close();
		 outStream.write(byteStream);
		 outStream.flush();
		 outStream.close();
	*/	
		// return image;
		 }
		 catch (IOException e)
		 {
		 System.err.println("Problem occurred creating chart.");
		 }
	//return image;
	
	 return Response.ok(image).build();
	
    	//return "image";
    	
    }
   
    
    @Path("pdf")
    @GET 
    @Produces("application/pdf")
    public File getpdf()
    {
    	 
     	DefaultCategoryDataset dataset = new DefaultCategoryDataset();
 		dataset.setValue(6, "Profit1", "Jane");
 		dataset.setValue(3, "Profit2", "Jane");
 		dataset.setValue(7, "Profit1", "T");
 		dataset.setValue(10, "Profit2", "T");
 		dataset.setValue(8, "Profit1", "Jill");
 		dataset.setValue(8, "Profit2", "Jill");
 		dataset.setValue(5, "Profit1", "John");
 		dataset.setValue(6, "Profit2", "John");
 		dataset.setValue(12, "Profit1", "F");
 		dataset.setValue(5, "Profit2", "F");
 		
 		// Profit1, Profit2 represent the row keys
 		// Jane, Tom, Jill, etc. represent the column keys
 		
 		JFreeChart chart = ChartFactory.createBarChart3D(
 		   "Comparison between Salesman",  // Chart name
 		   "Salesman",                     // X axis label
 		   "Value ($)",                    // Y axis value
 		   dataset,                        // data set
 		   PlotOrientation.VERTICAL,
 		   true, true, false);
    	//writeChartToPDF(chart,600,600,image);
    	/*public static void main(String[] args) {
    		writeChartToPDF(generateBarChart(), 500, 400, "C://barchart.pdf");
    		writeChartToPDF(generatePieChart(), 500, 400, "C://piechart.pdf");
    	}*/
    	//public  void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) {
    		PdfWriter writer = null;
    		File image =new File("vipul.pdf");
    		Document document = new Document();

    		try {
    			writer = PdfWriter.getInstance(document, new FileOutputStream(image));
    			document.open();
    			PdfContentByte contentByte = writer.getDirectContent();
    			PdfTemplate template = contentByte.createTemplate(600, 600);
    			Graphics2D graphics2d = template.createGraphics(600, 600,
    					new DefaultFontMapper());
    			Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, 600,600);

    			chart.draw(graphics2d, rectangle2d);

    			graphics2d.dispose();
    			contentByte.addTemplate(template, 0, 0);

    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		document.close();
    	//}
    	
    	
    	
    	/*String FILE = "c:/temp/FirstPdf.pdf";
    	Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
    			Font.BOLD);
    	Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
    			Font.BOLD);
    	Document document = new Document();
    	PdfWriter writer;
    	File file = img;
    	writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    	document.open();

    		try {
    			Document document = new Document();
    			PdfWriter.getInstance(document, new FileOutputStream(FILE));
    			document.open();
    			
    			document.addTitle("SMI");
    			document.addSubject("JfreeChart");
    			document.addKeywords("Jsp, PDF, iText");
    			document.addAuthor("vipul,linesh,parthesh,bhumika");
    			document.addCreator("vipul,linesh,parthesh,bhumika");
    			
    			Paragraph preface = new Paragraph();
    		//	addEmptyLine(preface, 1);
    			preface.add(new Paragraph("System Monitoring & Inventory ", catFont));
    			
    			
    		//	addEmptyLine(preface, 1);
    			preface.add(new Paragraph(
    					"Report generated by: " + System.getProperty("user.name") + ", " + new Date(),smallBold));
    			//InputStream is = img.getInputStream();
    		//	BufferedImage image = ImageIO.read(is);
    			File f=img;
    			BufferedImage image = ImageIO.read(f);
    			 Image i;
    			 
    			
    			document.add(preface);
    			document.newPage();

    			document.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	

    		*/
    		return image;
    }

	
}